<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人主页</el-breadcrumb-item>
      <el-breadcrumb-item>我的借阅</el-breadcrumb-item>
    </el-breadcrumb>

    <!--卡片视图区域-->
    <el-card :data="filterTableData" class="box-card">
      <!--搜索与添加区域-->
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <!--第一列-->
        <el-col :span="10">
          <!-- eslint-disable-next-line vue/no-parsing-error-->
          <el-input
            v-model="search"
            placeholder="请输入咨询事项的内容"
            clearable
            @clear="searchBtn"
          >
            <template #append>
              <el-button @click="searchBtn">搜</el-button>
            </template>
          </el-input>
        </el-col>
      </el-row>
    </el-card>

    <!--列表区域-->
    <el-card class="box-card2">
      <el-table
        ref="filterTable"
        :data="tableData"
        style="width: 100%"
        max-height="430"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="图书名称" prop="BOOK_NAME" width="180">
        </el-table-column>
        <el-table-column label="馆藏编号" prop="BOOK_ID">
          <template #default="scope">
            <el-tag disable-transitions> {{ scope.row.BOOK_ID }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="馆藏地址" prop="PLACE"> </el-table-column>
        <!-- <el-table-column label="出版社" prop="publisher"> </el-table-column> -->
        <el-table-column label="借书时间" prop="BORROW_TIME"> </el-table-column>
        <el-table-column label="应还时间" prop="RETURN_TIME"> </el-table-column>

        <el-table-column label="状态" prop="mode">
          <template #default="scope">
            <el-tag
              disable-transitions
              effect="dark"
              :type="buttonType(scope.row.STATE)"
            >
              {{ scope.row.STATE }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template #default="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleReturn(scope.$index, scope.row)"
              :disabled="scope.row.STATE!='已借出' ? true:false"
              >归还</el-button
            >
          </template>
        </el-table-column>
        <el-table-column align="right">
          <template #default="scope">
            <el-button
              type="primary"
              size="mini"
              @click="handleReBorrow(scope.$index, scope.row)"
              :disabled="scope.row.STATE!='已借出' ? true:false"
              >续借</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { computed, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { h } from "vue";
import axios from "axios";
const search = ref("");

export default {
  inject: ["reload"],
  data() {
    return {
      search: "",
      tableData: [],
      choosedate: "",
      userid: window.sessionStorage.getItem("userid"),
    };
  },
  computed: {
    tableData() {
      let search = this.search;
      let list = this.tableData.filter(
        (data) =>
          !search ||
          data.BOOK_NAME.toLowerCase().includes(search.toLowerCase()) ||
          data.BOOK_NAME.toLowerCase().includes(search.toLowerCase())
      );
      return list;
    },
  },

  created() {
    this.getBorrowList();
    this.getdate();
  },
  methods: {
    getdate() {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      this.choosedate = year + "-" + month + "-" + strDate;
    },
    getBorrowList() {
      axios({
        method: "post",
        params: {
          userid: this.userid,
        },
        url: "/api/LoanBooks/get_loan_information_by_userid",
      }).then((res) => {
        console.log(res);

        this.$data.tableData = res.data;
        this.tableData.forEach((item, i) => {
          item.BORROW_TIME = item.BORROW_TIME.substring(0, 10);
          item.RETURN_TIME = item.RETURN_TIME.substring(0, 10);
        });
      });
    },
    handleReturn(index, row) {
      if (row.STATE == "已借出") {
        axios({
          method: "post",
          params: {
            bookid: row.BOOK_ID,
            back_time: this.choosedate,
            back_people_id: this.userid,
          },
          url: "/api/BookBack/INSERTBOOKBACK",
        }).then((res) => {
        
           if (res.data) {
          ElMessage({
            message: "归还成功",
            type: "success",
          });
        }
        });
       
        this.reload();
      }
    },
    handleReBorrow(index, row) {
     
      {
ElMessageBox({
        title: "确认续借",
        message: h("p", null, [
          h("div", null, "续借图书：", row.BOOK_NAME),
          h("div", null, "馆藏地址：", row.PLACE),
          h("div", null, "续借时长：", "30天"),
        ]),
        showCancelButton: true,
        confirmButtonText: "确认续借",
        cancelButtonText: "取消",

        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            ElMessage({
              type: "success",
              message: "续借成功",
            });
            done();
          } else {
            ElMessage({
              message: "已取消续借",
            });
            done();
          }
        },
      }).then((action) => {
        if (action === "confirm") {
          console.log("commit");
          //向后端传输借书信息
          axios({
            method: "post",
            params: {          
              bookid:row.BOOK_ID,           
            },
            url: "/api/LoanBooks/renew_loan_time",
          }).then((res) => {
          console.log(res);
        });
          this.reload();
        }
      });
      
      
      }
      
    },
    //监听pagesize_3改变的事件
    handleSizeChange(newSize) {
      console.log(newSize);
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
    },
    buttonType(modeId) {
      // 选中的button更改颜色，其余的都更改为灰色
      if (modeId === "已借出") {
        return "success";
      } else if (modeId === "已归还") {
        return "danger";
      } else {
        return "info";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.box-card {
  margin-top: 20px;
  margin-bottom: 10px;
  font-size: 20px;
  margin-left: 40px;
  margin-right: 60px;
}
.box-card2 {
  margin-top: 20px;
  margin-bottom: 10px;
  font-size: 20px;
  margin-left: 40px;
  margin-right: 60px;
}
</style>